#pragma once
/**
 * 回显操作
*/
#include "../server.hpp"

class EchoSever
{
public:
    EchoSever(int port):_server(port)
    {
        _server.SetThreadCount(2);
        _server.EnableInactiveRelease(10);
        _server.SetClosedCallback(std::bind(&EchoSever::OnClose,this,std::placeholders::_1));
        _server.SetConnectedCallback(std::bind(&EchoSever::OnConnected,this,std::placeholders::_1));
        _server.SetMessageCallback(std::bind(&EchoSever::OnMessage,this,std::placeholders::_1,std::placeholders::_2));
    }
    void Start()
    {
        _server.Start();
    }
private:
    void OnConnected(const PtrConnection &conn)
    {
        logMessage(DEBUG,"new connection: %p",conn.get());
    }
    void OnClose(const PtrConnection &conn)
    {
        logMessage(DEBUG,"close connection: %p",conn.get());
    }
    void OnMessage(const PtrConnection &conn,Buffer* buf)
    {
        conn->Send(buf->ReadPosition(),buf->ReadAbleSize());
        buf->MoveReadOffest(buf->ReadAbleSize());
        conn->ShutDown();
    }
private:
    TcpServer _server;
};